package com.example.demo.Controller;

import com.example.demo.entity.Course;
import com.example.demo.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

    @Controller
    @RequestMapping("/StudentInfo")
    public class DesignController {
        private Student s;
        private List<Course> checkList;

        @GetMapping("/addStudentInfo")
        public String addStudentInfo(Model model){
            //跳转到学生个人信息页面
            Student s=new Student();
            model.addAttribute("StudentInfo",s);

            return "StudentInfo";
        }

        @GetMapping("/selectCourse")
        public String selectCourse(@ModelAttribute("StudentInfo") Student student, Errors errors, Model model){

            if(errors.hasErrors()){
                return "StudentInfo";
            }


            //实例化学生对象
            s=new Student();
            s=student;


            //添加课程
            List<Course> courses=new ArrayList<>();
            courses.add(new Course("语文"));
            courses.add(new Course("数学"));
            courses.add(new Course("英语"));
            courses.add(new Course("物理"));
            courses.add(new Course("化学"));
            courses.add(new Course("生物"));

            model.addAttribute("StudentInfo",s);
            model.addAttribute("courses",courses);

            return "SelectCource";
        }

        @PostMapping("/submitCourse")
        public String SelectResult(Model model,HttpServletRequest request){
            checkList=new ArrayList<>();
            String[] checkCourses=request.getParameterValues("checkCourses");
            for(int i=0;i<checkCourses.length;i++)
                checkList.add(new Course(checkCourses[i]));

            s.setCourseList(checkList);



            model.addAttribute("StudentInfo",s);
            model.addAttribute("courses",checkList);
            return "SelectResult";
        }
    }

